$(function(){
	var curPage = 1;
    var pageSize = 10;
	function formatTime(time) {
		if(time) {
			var date = new Date(time);
			var year = date.getFullYear();
			var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
			var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
			var hour = date.getHours() < 10 ? '0' + date.getHours():date.getHours();
			var min = date.getMinutes()<10 ? '0' + date.getMinutes() : date.getMinutes();
			return year + '-' + month + '-' + day + ' ' + hour + ':' + min;
		} else {
			return '永久有效';
		}
	}
	var point = {
		getList: function() {
			$.ajax({
			    url: wsCommon.host + '/api/memberPoint/list',
			    dataType: wsCommon.jsonType,
				data: {
                    pageSize: pageSize
                },
			    success: function(res) {
					if(res.code == 1) {
						if(res.result) {
							var pager = res.data.pager;

							point.initPaging(pager.pages, pager.total, res);

							$('[data-role="yx-point"]').html(res.data.point)
							$('[data-role="xf-point"]').html(res.data.userPointFreeze)
							$('[data-role="hq-point"]').html(res.data.gainPointFreeze)
						} else {
							wsCommon.setCookie('go_back_link', window.location.href);
							window.location.href = './passport-login.html';
						}
					}
			    },
			    error: function() {
			        // console.log('接口返回失败');
			    }
			})
		},
        initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                point.randerList(pageData);
                            } else {
                                curPage = page_index + 1;
                                point.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                }else {
                    ele.hide();
                    $('[data-role="tips"]').show();
                }
        },
        getPageDates: function() {
            var self = this;

            $.ajax({
                url: wsCommon.host + 'api/memberPoint/list',
                dataType: wsCommon.jsonType,
                data: {
					pageNum: curPage,
					pageSize: pageSize
				},
                success: function(res) {

                    // 同步页码
                    var totalPage = res.data.pager.pages;

                    var pageData = res.data.pager.list;
                    var pager = res.data.pager;
                    self.randerList(pageData);
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
        randerList: function(pageData) {
            var tpl = '';
            $.each(pageData, function(index, item) {
				var classs = item.changePoint > 0 ? 'text-available' : 'text-unavailable';
                tpl += '<tr class="text-disabled first">\
						  <td>'+formatTime(item.addtime)+'</td>\
						  <td><span class="'+classs+'">'+item.changePoint+'</span></td>\
						  <td>'+formatTime(item.expiretime)+'</td>\
						  <td>'+item.remark+'</td>\
						</tr>';
            })
            $('[data-role="result"]').html(tpl);
        },

		init: function() {
			this.getList();
		}
	}

	point.init();
})
